package com.cyms.modules.card.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.cyms.common.annotation.SysLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cyms.modules.card.entity.EtcEntity;
import com.cyms.modules.card.entity.EtcPayEntity;
import com.cyms.modules.card.service.EtcPayService;
import com.cyms.modules.card.service.EtcService;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.R;



/**
 * ETC卡消费记录
 *
 * @author low
 * @email tolower@foxmail.com
 * @date 2018-12-08 14:54:32
 */
@RestController
@RequestMapping("card/etcpay")
public class EtcPayController {
    @Autowired
    private EtcPayService etcPayService;
    @Autowired
    private EtcService etcService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = etcPayService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
			EtcPayEntity etcPay = etcPayService.selectById(id);

        return R.ok().put("etcPay", etcPay);
    }

    /**
     * 保存
     */
    @SysLog("新增ETC卡消费信息")
    @RequestMapping("/save")
    public R save(@RequestBody EtcPayEntity etcPay){
    	etcPay.setRecordDate(new Date());
    	etcPay.setCreateDate(new Date());
    	EtcEntity etc= etcService.selectById(etcPay.getCardId());
    	if (etc.getRestMoney().compareTo(etcPay.getMoney()) < 0) {
			return R.error("当前ETC卡内余额不足");
		}
    	etcPayService.insert(etcPay);
    	etc.setTotalPay(etc.getTotalPay().add(etcPay.getMoney()));
    	etc.setRestMoney(etc.getRestMoney().subtract(etcPay.getMoney()));
    	etcService.updateById(etc);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改ETC卡消费信息")
    @RequestMapping("/update")
    public R update(@RequestBody EtcPayEntity etcPay){
			etcPayService.updateById(etcPay);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除ETC卡充值记录")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
			etcPayService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
